package rsax

import (
	"crypto"
	"crypto/rsa"
	"crypto/sha1"
	"crypto/sha256"
)

// Sha1Sign Sha1加签
func Sha1Sign(key *rsa.PrivateKey, data []byte) ([]byte, error) {
	hashed := sha1.Sum(data)
	return Sign(key, crypto.SHA1, hashed[:])
}

// Sha256Sign Sha256加签
func Sha256Sign(key *rsa.PrivateKey, data []byte) ([]byte, error) {
	hashed := sha256.Sum256(data)
	return Sign(key, crypto.SHA256, hashed[:])
}

// Sign 私钥加签
func Sign(key *rsa.PrivateKey, hash crypto.Hash, hashed []byte) ([]byte, error) {
	return rsa.SignPKCS1v15(nil, key, hash, hashed)
}

// Sha1Verify Sha1 公钥验签
func Sha1Verify(key *rsa.PublicKey, hashed []byte, sig []byte) error {
	return rsa.VerifyPKCS1v15(key, crypto.SHA1, hashed, sig)
}

// Sha256Verify Sha256 公钥验签
func Sha256Verify(key *rsa.PublicKey, hashed []byte, sig []byte) error {
	return rsa.VerifyPKCS1v15(key, crypto.SHA256, hashed, sig)
}

// Verify 公钥加签
func Verify(key *rsa.PublicKey, hash crypto.Hash, hashed []byte, sig []byte) error {
	return rsa.VerifyPKCS1v15(key, hash, hashed, sig)
}
